<?
/* 
 *  C端用户下单处理页面
 *
 */
if(!$_SESSION['memberID'] && !$_SESSION['domain_mid']) exit;
include("/var/www/html/new/com.inc.php");
include("/var/www/html/new/d/common/errBack.php");
include("/var/www/html/new/d/common/err_code.php");
include("/var/www/html/new/d/class/Sale.class.php");
include("/var/www/html/new/d/class/Member.php");
include("/var/www/html/new/d/class/ProductInfo.php");
include("/var/www/html/new/d/class/DisOrder.php");
include("/var/www/html/new/d/class/DisOrder_c.php");
// include("/var/www/html/new/conf/yd.le.je");
include_once("/var/www/html/new/d/common/yd.le.je");

$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

// 会员ID为二级域名绑定的分销账户
$memberID = $_SESSION['domain_mid'];

if(p_match($ordername=$_REQUEST['ordername']))  exit("取票人含有非法字符");
if(p_match($ordertel=$_REQUEST['ordertel']))  exit("取票人电话含有非法字符");
if(!$_REQUEST['idCard']) exit("请输入身份证号码");
$data = array();
$post = $_POST;
$idCard=$_REQUEST['idCard'];
$ordername = $_POST['ordername'];
$ordertel = $_POST['ordertel']+0;
$memo = $_POST['memo'];
// print_r($_POST);
foreach($post['pid'] as $k=>$v){
    if($_POST['tnum'][$k]==0) continue; // 门票数量为0不购买
    $arr = array(
        'pid'=>$v,
        'begintime'=>$_POST['begintime'][$k],
        'ordertel'=>$ordertel,
        'ordername'=>$ordername,
        'tnum'=>$_POST['tnum'][$k],
        'remoteorder'=>'',
        'paymode'=>1,
        'memo'=>$memo,
        'leavetime'=>$_POST['endtime'][$k],
        'idCard'=>$idCard,
        'apply_did'=>$_POST['aid'][$k],
        'contacttel'=>$_SESSION['account']
    );
    // 判断是否餐饮类型，是一个
    if(isset($post['ptype'][$k]) && ($post['ptype'][$k]=='C'||$post['ptype'][$k]=='G')){
        $data[$k]= $arr;
    }else{
        if(isset($data[$post['lid'][$k]]) && ($post['begintime'][$k]==$data[$post['lid'][$k]]['begintime'])){ // 如果预定的是同一天的可以当联票
            $data[$post['lid'][$k]]['c_pids'][$v]=$_POST['tnum'][$k];
        }else{
            if(isset($data[$post['lid'][$k]]) && ($post['begintime'][$k]!=$data[$post['lid'][$k]]['begintime'])){
                $data[$k]= $arr;
                continue;
            }
            $data[$post['lid'][$k]]= $arr;
        }
    }
}


foreach($data as $v){
    $pro=new ProductInfo($s,$v['pid'],$v['apply_did']);
    $m=new Member($s,$memberID);
    $sOrder=new SaleOrderC($s,$pro,$m);
    try{
        $orderData[]=$sOrder->beforeCheck($v);
    }catch(Exception $e){
        echo '<script>alert("'.$e->getMessage().'")</script>';
        exit;
    }
    
}

$pids = implode(',',$_POST['pid']);
$sql="select id,p_name from uu_products where id in ($pids)";
$GLOBALS['le']->query($sql);
$product = array();
while($row=$GLOBALS['le']->fetch_assoc()){
    $product[$row['id']] = $row['p_name'];
}

$ordernum = array();
$ordertmp = array();

$totalPrice = 0;
// print_r($orderData);
// exit;
foreach($orderData as $v){
    $totalPrice +=$v['main']['totalTprice'];
    try{
        $ordertmp[]=$ordernum[$v['option']['pid']]=$sOrder->createOrder($v,$s);
    }catch(Exception $e){
        echo '<script>alert("'.$e->getMessage().'")</script>';
        exit;
    }
    
}

// 清空购物车
$ydle=new yd_go_sql();
$ydle->connect(); 
$sql="delete from uu_shoppingcart where fid={$_SESSION['memberID']}";
$ydle->query($sql);
$url = '/new/d/shops/gorderSuc_dev.html';
if(count($ordertmp)>1){
    $newOrder = 'st_'.$ordertmp[0];
    $subOrder = implode(',',$ordertmp);
    $sql="INSERT INTO uu_unitpay (new,old,status,tprice) values ('$newOrder','$subOrder',0,$totalPrice)";
    if($ydle->query($sql)){
        header('Location:'.$url.'?ordernum='.$newOrder.'&t='.$totalPrice);
        exit;
    }
}

header('Location:'.$url.'?ordernum='.$ordertmp[0].'&t='.$totalPrice);
?>